package com.zhonghui.license;

import java.util.Hashtable;
import org.apache.logging.log4j.LogManager;
import java.util.Properties;
import org.springframework.core.io.Resource;
import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;
import org.apache.commons.lang3.StringUtils;
import java.util.Iterator;
import java.util.List;
import java.io.UnsupportedEncodingException;
import java.beans.XMLDecoder;
import java.beans.ExceptionListener;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import de.schlichtherle.license.LicenseContentException;
import java.util.Date;
import de.schlichtherle.license.NoLicenseInstalledException;
import de.schlichtherle.xml.GenericCertificate;
import de.schlichtherle.license.LicenseNotary;
import de.schlichtherle.license.LicenseContent;
import de.schlichtherle.license.LicenseParam;
import org.springframework.beans.factory.annotation.Value;
import org.apache.logging.log4j.Logger;
import de.schlichtherle.license.LicenseManager;

public class CustomLicenseManager extends LicenseManager
{
    private static Logger logger;
    private static final String XML_CHARSET = "UTF-8";
    private static final int DEFAULT_BUFSIZE = 8192;
    @Value("${product.url}")
    private String url;
    
    public CustomLicenseManager() {
    }
    
    public CustomLicenseManager(final LicenseParam param) {
        super(param);
    }
    
    protected synchronized byte[] create(final LicenseContent content, final LicenseNotary notary) throws Exception {
        this.initialize(content);
        this.validateCreate(content);
        final GenericCertificate certificate = notary.sign((Object)content);
        return this.getPrivacyGuard().cert2key(certificate);
    }
    
    protected synchronized LicenseContent install(final byte[] key, final LicenseNotary notary) throws Exception {
        final GenericCertificate certificate = this.getPrivacyGuard().key2cert(key);
        notary.verify(certificate);
        final LicenseContent content = (LicenseContent)this.load(certificate.getEncoded());
        this.validate(content);
        this.setLicenseKey(key);
        this.setCertificate(certificate);
        return content;
    }
    
    protected synchronized LicenseContent verify(final LicenseNotary notary) throws Exception {
        GenericCertificate certificate = this.getCertificate();
        final byte[] key = this.getLicenseKey();
        if (null == key) {
            throw new NoLicenseInstalledException(this.getLicenseParam().getSubject());
        }
        certificate = this.getPrivacyGuard().key2cert(key);
        notary.verify(certificate);
        final LicenseContent content = (LicenseContent)this.load(certificate.getEncoded());
        this.validate(content);
        this.setCertificate(certificate);
        return content;
    }
    
    protected synchronized void validateCreate(final LicenseContent content) throws LicenseContentException {
        final LicenseParam param = this.getLicenseParam();
        final Date now = new Date();
        final Date notBefore = content.getNotBefore();
        final Date notAfter = content.getNotAfter();
        if (null != notAfter && now.after(notAfter)) {
            throw new LicenseContentException("证书失效时间不能早于当前时间");
        }
        if (null != notBefore && null != notAfter && notAfter.before(notBefore)) {
            throw new LicenseContentException("证书生效时间不能晚于证书失效时间");
        }
        final String consumerType = content.getConsumerType();
        if (null == consumerType) {
            throw new LicenseContentException("用户类型不能为空");
        }
    }
    
    protected synchronized void validate(final LicenseContent content) throws LicenseContentException {
        super.validate(content);
    }
    
    private Object load(final String encoded) {
        BufferedInputStream inputStream = null;
        XMLDecoder decoder = null;
        try {
            inputStream = new BufferedInputStream(new ByteArrayInputStream(encoded.getBytes("UTF-8")));
            decoder = new XMLDecoder(new BufferedInputStream(inputStream, 8192), null, null);
            final Object var4 = decoder.readObject();
            return var4;
        }
        catch (UnsupportedEncodingException var5) {
            var5.printStackTrace();
            try {
                if (decoder != null) {
                    decoder.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            }
            catch (Exception var6) {
                CustomLicenseManager.logger.error("XMLDecoder解析XML失败", (Throwable)var6);
            }
        }
        finally {
            try {
                if (decoder != null) {
                    decoder.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            }
            catch (Exception var7) {
                CustomLicenseManager.logger.error("XMLDecoder解析XML失败", (Throwable)var7);
            }
        }
        return null;
    }
    
    private LicenseCheckModel getServerInfos() {
        final String osName = System.getProperty("os.name").toLowerCase();
        AbstractServerInfos abstractServerInfos = null;
        if (osName.startsWith("windows")) {
            abstractServerInfos = new WindowsServerInfos();
        }
        else if (osName.startsWith("linux")) {
            abstractServerInfos = new LinuxServerInfos();
        }
        else {
            abstractServerInfos = new LinuxServerInfos();
        }
        return abstractServerInfos.getServerInfos();
    }
    
    private boolean checkIpAddress(final List<String> expectedList, final List<String> serverList) {
        if (expectedList != null && expectedList.size() > 0) {
            if (serverList != null && serverList.size() > 0) {
                for (final String expected : expectedList) {
                    if (serverList.contains(expected.trim())) {
                        return true;
                    }
                }
            }
            return false;
        }
        return true;
    }
    
    private boolean checkSerial(final String expectedSerial, final String serverSerial) {
        return !StringUtils.isNotBlank((CharSequence)expectedSerial) || (StringUtils.isNotBlank((CharSequence)serverSerial) && expectedSerial.equals(serverSerial));
    }
    
    public static Object getBootstrapYml(final Object key) {
        final Resource resource = (Resource)new ClassPathResource("/config/application.yml");
        Properties properties = null;
        try {
            final YamlPropertiesFactoryBean yamlFactory = new YamlPropertiesFactoryBean();
            yamlFactory.setResources(new Resource[] { resource });
            properties = yamlFactory.getObject();
        }
        catch (Exception var4) {
            var4.printStackTrace();
            return null;
        }
        return (properties).get(key);
    }
    
    static {
        CustomLicenseManager.logger = LogManager.getLogger((Class)CustomLicenseManager.class);
    }
}
